package com.snail.jj.androiddemos.foundationwidgets;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.snail.jj.androiddemos.R;
import com.snail.jj.androiddemos.bases.BaseActivity;
import com.snail.jj.androiddemos.beans.Person;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by snail on 2017/4/19.
 */

public class ListViewActivity extends BaseActivity {

    private ListView listView;
    private ArrayList<Person> persons;
    private ArrayList<Map<String,Object>> mData = new ArrayList<Map<String,Object>>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foundation_listview);
        initData();
        initView();
    }

    public void initData() {
        persons = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.name = person.name + i;
            person.age = person.age + i;
            persons.add(person);
        }
    }

    public void initView() {
        setTitleBarTitle("ListView");
        setRightViewVisiable(View.INVISIBLE);
        listView = (ListView) findViewById(R.id.id_listView_list1);
        listView.setBackgroundColor(Color.RED);
//        ArrayAdapter<Person> adapter = new ArrayAdapter<Person>
//                (ListViewActivity.this,R.layout.support_simple_spinner_dropdown_item,persons);

        SimpleAdapter adapter1 = new SimpleAdapter(this,dealData(),R.layout.listviewitem,
                new String[]{"title","weight"},
                new int[]{R.id.listViewItemTitle,R.id.listViewItemSubtitle});
        listView.setAdapter(adapter1);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Person person = persons.get(position);
            }
        });

    }



    public List<Map<String,Object>> dealData() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for(Person p : persons) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("title",p.name);
            map.put("weight",p.weight);
            list.add(map);
        }
        return list;
    }
}
